package core.sb;

import core.sb.s.Seq;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.LinkedList;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class SB implements Serializable{
    private LinkedList <Seq> seq;
    public static char NUCS  [] = {'A', 'T', 'G', 'C', '-', '.'};

    public SB(){ seq = new LinkedList();}

    public SB(String[] as){
        seq = new LinkedList();
        for (int x = 0; x < as.length; x++){seq.add(new Seq(x + "", new String(as[x])));}
    }

    public void rm(int n){seq.remove(seq.get(n));}
    public void add(Seq seq){ this.seq.add(seq);}
    public int getSize(){return seq.size();}
    public Seq get(int n){return seq.get(n);}

    public Seq getSeqByI(int i){
        for(int x=0; x<seq.size(); x++){if(seq.get(x).getI() == i){return seq.get(x);}}
        return null;
    }

    public void rmDups(){
        for (int y = 0; y < this.getSize(); y++){
            for (int t = this.getSize() - 1; t >= y + 1; t--) {
                if(this.get(y).getDNA().equals(this.get(t).getDNA())){this.rm(t);}
            }
        }
    }

    public void rmSeqsWithNoInfo(){
        for (int t = this.getSize() - 1; t >= 0; t--) {
            boolean remove = true;
            if(this.get(t).getDNA().indexOf("A")!=-1){remove = false;}
            else if(this.get(t).getDNA().indexOf("T")!=-1){remove = false;}
            else if(this.get(t).getDNA().indexOf("G")!=-1){remove = false;}
            else if(this.get(t).getDNA().indexOf("C")!=-1){remove = false;}
            if(remove==true){
                this.rm(t);
            }
        }
    }


    public void saveAsFasta(String pathAndFname) throws Exception{
        PrintWriter outFile = new PrintWriter(new FileWriter(pathAndFname), false);
        for(int x=0; x<seq.size(); x++){
            outFile.println(">"+seq.get(x).getTitle());
            outFile.println(seq.get(x).getDNA());
        }
        outFile.close();
    }

    public void loadFasta(String pathAndFname){
        seq = new LinkedList();
        String seqTitle = "";
        String sequence = "";
        try{
            boolean again = true;
            BufferedReader inputFile = new BufferedReader(new FileReader(pathAndFname));
            do{
                try{
                    String tempLine = new String(inputFile.readLine());
                    if (!tempLine.trim().equals("")) {
                        if(tempLine.trim().charAt(0) == '>'){
                            if(!sequence.equals("")){
                                seq.add(new Seq(seqTitle.toUpperCase().trim().replace(' ', '_').replace(">", ""), sequence));
                                seqTitle = "";
                                sequence = "";
                            }
                            seqTitle = tempLine;
                        }
                        else{sequence = sequence + tempLine.trim();}
                    }
                }
                catch(Exception theException){
                    again = false;
                    seq.add(new Seq(seqTitle.toUpperCase().trim().replace(' ', '_').replace(">", ""), sequence));                }
            }while(again);
            inputFile.close();
        }
        catch(Exception nfne){}
    }
}